package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"quiz-master/cache"
	"quiz-master/model"
	"strconv"
	"time"
)

func RepAdd(c *gin.Context) {
	repcode := c.PostForm("repcode")
	repname := c.PostForm("repcode")
	if repcode == "" || repname == "" {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "字段不可为空",
		})
		return
	}
	data := model.Repo{
		RepCode: repcode,
		RepName: repname,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func RepOrderAdd(c *gin.Context) {
	rep_id, _ := strconv.Atoi(c.PostForm("rep_id"))
	order_code := c.PostForm("order_code")
	order_price, _ := strconv.Atoi(c.PostForm("order_price"))
	good_num, _ := strconv.Atoi(c.PostForm("good_num"))
	if rep_id == 0 || order_code == "" || good_num == 0 || order_price == 0 {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "字段不能为空",
		})
		return
	}
	data := model.RepOrder{
		RepId:      rep_id,
		OrderCode:  order_code,
		GoodNum:    good_num,
		OrderPrice: order_price,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func RepoOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size
	var list []model.RepoOrderList
	redis_key := "repo_order_list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("rep_orders").
			Select("rep_orders.order_id as OrderId",
				"repos.rep_code as RepCode",
				"repos.rep_name as RepName",
				"rep_orders.order_code as OrderCode",
				"rep_orders.good_num as GoodNum",
				"rep_orders.order_price as OrderPrice").
			Joins("LEFT JOIN repos ON repos.id=rep_orders.rep_id").
			Offset(offset).
			Limit(size).
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	/*
		SELECT
		rep_orders.order_id as OrderId,
		 repos.rep_code as RepCode,
		 repos.rep_name as RepName,
		 rep_orders.order_code as OrderCode,
		 rep_orders.good_num as GoodNum,
		 rep_orders.order_price as OrderPrice
		from rep_orders
		LEFT JOIN repos ON repos.id=rep_orders.rep_id
	*/

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}

func RepoOrderDetail(c *gin.Context) {
	rep_id := c.Query("rep_id")
	var repOrder model.RepoOrderList
	redis_key := "repo_detail_" + rep_id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("rep_orders").
			Select("rep_orders.order_id as OrderId",
				"repos.rep_code as RepCode",
				"repos.rep_name as RepName",
				"rep_orders.order_code as OrderCode",
				"rep_orders.good_num as GoodNum",
				"rep_orders.order_price as OrderPrice").
			Joins("LEFT JOIN repos ON repos.id=rep_orders.rep_id").
			Where("repos.id  = ?", rep_id).
			Find(&repOrder)
		jsonData, _ := json.Marshal(repOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &repOrder)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": repOrder,
	})
	return
}
